package com.yehongyu.mytools.core.system;

import java.io.InputStream;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 装载资源的工具类(用于通过不同的ClassLoader来定位资源)
 * @author yingyang
 * @since 2011-11-30
 */
public class ResourceLoader {
    private static Log log = LogFactory.getLog(ResourceLoader.class);
    
    /**
     * 根据ClassPath路径获取资源InputStream对象
     * @param resource
     * @return java.io.InputStream
     */
    public static InputStream getResourceAsStream(String resource){
    	ClassLoader classLoader = null;
    	InputStream is = null;
    	if(log.isDebugEnabled()){
    		log.debug("getResourceAsStream资源路径:" + resource);
    	}
    	//先获取当前类Class的ClassLoader
        classLoader = ResourceLoader.class.getClassLoader();
        if (classLoader != null) {
            is = classLoader.getResourceAsStream(resource);
            if (is != null) {
            	if(log.isDebugEnabled()){
            		log.debug("从当前类Class的ClassLoader中获取到InputStream资源");
            	}
                return is;
            }
        }
        //在获取当前线程Context的CalssLoader
        classLoader = Thread.currentThread().getContextClassLoader();
        if (classLoader != null) {
            is = classLoader.getResourceAsStream(resource);
            if (is != null) {
            	if(log.isDebugEnabled()){
            		log.debug("从当前线程Context的CalssLoader中获取到InputStream资源");
            	}
                return is;
            }
        }
        //最后获取系统的ClassLoader
        is = ClassLoader.getSystemResourceAsStream(resource);
        if (is != null) {
        	if(log.isDebugEnabled()){
        		log.debug("从系统的ClassLoader中获取到InputStream资源");
        	}
            return is;
        }else{
        	if(log.isDebugEnabled()){
        		log.debug("未获取到InputStream资源，返回Null！");
        	}
        	return null;
        }
    }

    /**
     * 根据ClassPath路径获取资源URL对象
     * @param resource
     * @return java.net.URL
     */
    public static URL getResource(String resource) {
        ClassLoader classLoader = null;
        URL url = null;
    	if(log.isDebugEnabled()){
    		log.debug("getResource资源路径:" + resource);
    	}
    	//先获取当前类Class的ClassLoader
        classLoader = ResourceLoader.class.getClassLoader();
        if (classLoader != null) {
            url = classLoader.getResource(resource);
            if (url != null) {
            	if(log.isDebugEnabled()){
            		log.debug("从当前类Class的ClassLoader中获取到URL资源");
            	}
                return url;
            }
        }
        //在获取当前线程Context的CalssLoader
        classLoader = Thread.currentThread().getContextClassLoader();
        if (classLoader != null) {
            url = classLoader.getResource(resource);
            if (url != null) {
            	if(log.isDebugEnabled()){
            		log.debug("从当前线程Context的CalssLoader中获取到URL资源");
            	}
                return url;
            }
        }
        //最后获取系统的ClassLoader
        url = ClassLoader.getSystemResource(resource);
        if (url != null) {
        	if(log.isDebugEnabled()){
        		log.debug("从系统的ClassLoader中获取到URL资源");
        	}
            return url;
        }else{
        	if(log.isDebugEnabled()){
        		log.debug("未获取到URL资源，返回Null！");
        	}
        	return null;
        }
    }
    
    public static void main(String[] args){
    	URL url = ResourceLoader.getResource("log4j.xml");
    	System.out.println(url);
    	InputStream is = ResourceLoader.getResourceAsStream("log4j.xml");
    	System.out.println(is);
    	System.out.println(ClassLoader.getSystemResource(""));
    }
}